﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;

namespace MisClient
{
    public partial class FrmRecordation : BaseForm
    {
        private IBLL.IDepart departBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteDepart();
        private IBLL.IGoods goodsBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteGoods();
        private IBLL.ISacf sacfBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteSacf();

        private WinPrint winPrint = new WinPrint();

        private int dataGridView = 0;

        public FrmRecordation()
        {
            InitializeComponent();
        }

        private void FrmRecordation_Load(object sender, EventArgs e)
        {
            this.openStarDate.Value = DateTime.Now.Date.AddDays(-7);
            this.txtDepCode.Text = accountUser.DeptCode;
            this.openStarDate.Focus();
        }

        private void txtDepCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (string.IsNullOrEmpty(this.txtDepCode.Text.Trim()))
                {
                    MessageBox.Show("部门代码不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtDepCode.Focus();
                    return;
                }
                MisModel.Depart depart = departBLL.GetById(this.txtDepCode.Text.Trim());
                if (depart == null)
                {
                    MessageBox.Show("此部门无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtDepCode.Focus();
                    return;
                }
                this.txtBoxDep.Text = depart.DepName;
                this.txtID.Focus();
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtDepCode.Text.Trim()))
            {
                MessageBox.Show("部门代码不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtDepCode.Focus();
                return;
            }
            MisModel.Depart depart = departBLL.GetById(this.txtDepCode.Text.Trim());
            if (depart == null)
            {
                MessageBox.Show("此部门无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtDepCode.Focus();
                return;
            }
            if (string.IsNullOrEmpty(this.txtID.Text.Trim()))
            {
                MessageBox.Show("商品不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtID.Focus();
                return;
            }
            MisEntity.Goods goods = goodsBLL.EndsWithID(this.txtID.Text.Trim());
            if (goods == null)
            {
                MessageBox.Show("无此商品,请先设置！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtID.Focus();
                return;
            }
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            StringBuilder sb = new StringBuilder();
            this.dataGridView1.Rows.Clear();
            sb.Append(" and dep_id=@depId");
            dictionary.Add("@depId", this.txtDepCode.Text.Trim());
            sb.Append(" and date>=@startDate and date<=@endDate");
            dictionary.Add("@startDate", this.openStarDate.Value.ToShortDateString());
            DateTime time = DateTime.Parse(this.openEndDate.Value.ToShortDateString());
            time = time.Date.AddDays(1);
            dictionary.Add("@endDate", time.ToShortDateString());
            dictionary.Add("@id", this.txtID.Text.Trim());
            DataTable dt = sacfBLL.GetRecordationAll(sb.ToString(), dictionary);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    String[] DataRow = new String[16];
                    DataRow[0] = dt.Rows[i]["date"].ToString();
                    DataRow[1] = dt.Rows[i][1].ToString();
                    DataRow[2] = dt.Rows[i]["no"].ToString();
                    DataRow[3] = dt.Rows[i]["sl"].ToString();
                    DataRow[4] = dt.Rows[i]["price_hs"].ToString();
                    DataRow[5] = Convert.ToDouble(dt.Rows[i]["pricei"].ToString()).ToString("f4");
                    DataRow[6] = dt.Rows[i]["je_hs"].ToString();
                    DataRow[7] = dt.Rows[i]["slkc"].ToString();
                    DataRow[8] = dt.Rows[i]["jekc"].ToString();
                    DataRow[9] = dt.Rows[i]["shop_id"].ToString();
                    DataRow[10] = dt.Rows[i]["cus_shop"].ToString();
                    DataRow[11] = dt.Rows[i]["dep_id"].ToString();
                    DataRow[12] = dt.Rows[i]["cus_dep"].ToString();
                    DataRow[13] = dt.Rows[i]["cus_id"].ToString();
                    DataRow[14] = string.Format("{0}{1}{2}{3}", dt.Rows[i]["f1"].ToString(), dt.Rows[i]["f2"].ToString(), dt.Rows[i]["f3"].ToString(), dt.Rows[i]["f4"].ToString());
                    DataRow[15] = dt.Rows[i]["bei"].ToString();
                    this.dataGridView1.Rows.Add(DataRow);
                }
            }
            else
            {
                MessageBox.Show("查无符合条件的记录！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
        }

        private void txtID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (string.IsNullOrEmpty(this.txtID.Text.Trim()))
                {
                    MessageBox.Show("商品不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtID.Focus();
                    return;
                }
                MisEntity.Goods goods = goodsBLL.EndsWithID(this.txtID.Text.Trim());
                if (goods == null)
                {
                    MessageBox.Show("无此商品,请先设置！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtID.Focus();
                    return;
                }
                this.txtGoods.Text = goods.GOODS;
                this.txtDM.Text = goods.DM;
                this.txtID.Text = goods.ID;
                this.btnOk.Focus();
            }
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnRef_Click(object sender, EventArgs e)
        {
            Point mousePoint = MousePosition;
            Point point = this.PointToClient(mousePoint);
            this.contextMenuStrip1.Show(this, point);
        }

        private void toolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("calc.exe");
        }

        private void btnPri_Click(object sender, EventArgs e)
        {
            this.dataGridView = 0;
            this.printPreviewDialog1.Document = this.printDocument1;
            this.printPreviewDialog1.PrintPreviewControl.Zoom = 1.0;
            this.printPreviewDialog1.ShowDialog();
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            string _Title = "新江厦汽车销售公司商品备查簿";
            Font _TitleFont = new Font("宋体", 20, FontStyle.Bold);
            Point _CurrentPoint = new Point(30,30);
            _CurrentPoint.X = (e.PageBounds.Width) / 2 - (int)(e.Graphics.MeasureString(_Title, _TitleFont).Width) / 2;
            e.Graphics.DrawString(_Title, _TitleFont, new SolidBrush(Color.Black), _CurrentPoint);
            StringBuilder sb = new StringBuilder();
            sb.Append("\n");
            sb.Append(string.Format("日期：{0} 至 {1}\n", this.openStarDate.Value.ToShortDateString(),this.openEndDate.Value.ToShortDateString()));
            sb.Append(string.Format("部门：{0}\n", this.txtDepCode.Text.Trim()));
            sb.Append(string.Format("商品：{0}\n", this.txtID.Text.Trim()));
            sb.Append("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
            sb.Append("日期                情况     单据号    数量    售价  进(变)价    金额  数量余额  金额余额\n");
            sb.Append("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
            if (this.dataGridView1.Rows.Count > 0)
            {
                DataGridViewRow dr = null;
                for (int i = dataGridView; i < this.dataGridView1.RowCount; i++, dataGridView++)
                {
                    dr = this.dataGridView1.Rows[i];
                    sb.Append(dr.Cells[0].Value.ToString()).Append(" ");
                    sb.Append(dr.Cells[1].Value.ToString()).Append(" ");
                    sb.Append(dr.Cells[2].Value.ToString());
                    sb.Append(winPrint.getLenStr(8, decimal.Parse(dr.Cells[3].Value.ToString()).ToString("f2")));
                    sb.Append(winPrint.getLenStr(8, decimal.Parse(dr.Cells[4].Value.ToString()).ToString("f2")));
                    sb.Append(winPrint.getLenStr(8, decimal.Parse(dr.Cells[5].Value.ToString()).ToString("f2")));
                    sb.Append(winPrint.getLenStr(9, decimal.Parse(dr.Cells[6].Value.ToString()).ToString("f2")));
                    sb.Append(winPrint.getLenStr(8, decimal.Parse(dr.Cells[7].Value.ToString()).ToString("f2")));
                    sb.Append(winPrint.getLenStr(10, decimal.Parse(dr.Cells[8].Value.ToString()).ToString("f2"))).Append("\n");
                    if (i != 0 && i % 44 == 0)
                    {
                        dataGridView++;
                        break;
                        //e.HasMorePages = true;
                        //return;
                    }
                }
            }
            sb.Append("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
            sb.Append(string.Format("制表：{0} 姓名{1}      日期：{2}", accountUser.UserCode, accountUser.Oper, DateTime.Now));

            _CurrentPoint.X = 30;
            _CurrentPoint.Y += (int)(e.Graphics.MeasureString(_Title, _TitleFont).Height)+20;
            Font _ContentFont = new Font("宋体", 12);
            e.Graphics.DrawString(sb.ToString(), _ContentFont, new SolidBrush(Color.Black), _CurrentPoint);
            if (dataGridView < this.dataGridView1.RowCount-1)
                e.HasMorePages = true;
            else
                e.HasMorePages = false;
        }
    }
}
